﻿using DiffPlex;
using DiffPlex.DiffBuilder;
using DiffPlex.DiffBuilder.Model;
using ICSharpCode.WinFormsUI.Controls;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DevelopAssistant.AddIn.Diffplex
{
    public partial class MainForm : ICSharpCode.WinFormsUI.Docking.DockContent
    {
        private bool read = false;
        private Form mainForm = null;

        OpenFileDialog openFile = new OpenFileDialog();
        ISideBySideDiffBuilder diffBuilder = null;

        public MainForm()
        {
            InitializeComponent();
            InitializeControls();
        }

        public MainForm(Form owner, AddInBase addin)
            : this()
        {
            mainForm = owner;
            diffBuilder = new DiffPlex.DiffBuilder.SideBySideDiffBuilder(new Differ());
        }

        private void InitializeControls()
        {
            this.panel3.BorderStyle = NBorderStyle.All;
            this.panel1.BorderColor = SystemColors.ControlLight;           
            this.panel2.BorderStyle = NBorderStyle.Bottom;
            this.panel3.BorderStyle = NBorderStyle.Bottom;
            this.panel2.BorderColor = SystemColors.ControlLight;
            this.panel3.BorderColor = SystemColors.ControlLight;

        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            this.comboBox1.SelectedIndex = 2;
            this.splitContainer1.SplitterDistance = this.splitContainer1.Width / 2;
            this.richTextBox1.Font = this.richTextBox2.Font = new Font("微软雅黑", 9.0F);

            this.openFile.Filter = "记事本|*.txt|SQL|*.sql|C Sharp|*.cs|HTML|*.html|XML|.xml|CSS|*.css|JS|*.js|Json|*.json|其他格式|*.*";
            this.richTextBox1.ScrollChanged += new ScrollEventPlasHandler(DoRichTextBox1Scolling);
            this.richTextBox2.ScrollChanged += new ScrollEventPlasHandler(DoRichTextBox2Scolling);

            //string input = "敬爱的：<font        name='微软雅黑'   onclick='alert(\"this is a demo\",\"提示信息\")'   size='12'  style='Italic|Bold|Underline'  title='2017-08-12\\s23:21:20'  color='blue'  >超级管理员</font>你好,<font name='微软雅黑'  onclick='OpenFile(\"我的文档\")' style='Bold'   color='red'    >2000000</font>   欢迎 第<font size='18' color='yellow' title='第50次访问' style='Bold'>50</font>次 登陆 end ";
            //input = "敬爱的：<font        name='微软雅黑'      size='12'  style='italic|bold| underline'  title='2017-08-12    23:21:20'  color='blue' >超级管理员</font>你好 欢迎登陆";
            //ICSharpCode.WinFormsUI.Forms.MessageForm.Show(mainForm, input, "提示信息", MessageBoxButtons.OKCancel, MessageBoxIcon.Information, new ICSharpCode.WinFormsUI.Forms.MessageBoxSize(440, 200), this);
        }

        public void alert(string msg,string title)
        {
            MessageBox.Show(msg + "" + title);
        }

        public void OpenFile(string file)
        {
            openFile.FileName = file;
            if (openFile.ShowDialog(this).Equals(DialogResult.OK))
            {

            }
        }

        private void SelectOpenFile(object sender, EventArgs e)
        {
            if (read) return;

            if (openFile.ShowDialog(this).Equals(DialogResult.OK))
            {
                if (!string.IsNullOrEmpty(openFile.FileName))
                {
                    Control control = (Control)sender;
                    string context = GetContent(openFile.FileName);
                    switch (control.Name)
                    {
                        case "lblFile1":
                            richTextBox1.Text = context;
                            break;
                        case "lblFile2":
                            richTextBox2.Text = context;
                            break;
                    }
                }
            }
        }

        private string GetContent(string path)
        {
            using (System.IO.StreamReader sr = new System.IO.StreamReader(path,Encoding.Default))
            {
                return sr.ReadToEnd();
            }           
        }

        private void btnApply_Click(object sender, EventArgs e)
        {
            if (!read)
            {
                string before = this.richTextBox1.Text;
                string after = this.richTextBox2.Text;

                string fontName="微软雅黑";
                switch (comboBox1.SelectedIndex)
                {
                    case 0:
                        fontName = "宋体";
                        break;
                    case 1:
                        fontName = "仿宋";
                        break;
                    case 2:
                        fontName = "微软雅黑";
                        break;
                }
                this.richTextBox1.Font = this.richTextBox2.Font = new Font(fontName, 9.0F);
                 
                DiffPlexViewer(diffBuilder.BuildDiffModel(before, after));

                read = true;
                btnApply.Text = "开始编辑";
                this.lblState1.Text = "(只读)";
                this.lblState2.Text = "(只读)";                              
            }
            else
            {
                string before = this.richTextBox1.Text;
                string after = this.richTextBox2.Text;

                DiffPlexViewer(null);

                read = false;
                btnApply.Text = "开始对比";
                this.lblState1.Text = "(编辑)";
                this.lblState2.Text = "(编辑)";                
            }
        }

        private void DiffPlexViewer(SideBySideDiffModel diff)
        {
            if (diff != null)
            {
                this.richTextBox1.SetRenderModel(diff.OldText);
                this.richTextBox2.SetRenderModel(diff.NewText);
            }
            else
            {
                this.richTextBox1.SetRenderModel(null);
                this.richTextBox2.SetRenderModel(null);
            }
        }

        private void DoRichTextBox1Scolling(object sender, int value, ScrollEventArgs e)
        {
            richTextBox2.DVScroll(sender, value, e);
        }

        private void DoRichTextBox2Scolling(object sender, int value, ScrollEventArgs e)
        {
            richTextBox1.DVScroll(sender, value, e);
        }

    }
}
